//
//  ACController.m
//  GKBHouse
//
//  Created by  on 12-7-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ACController.h"
#import "GKBNavigationBar.h"
#import "iToast.h"
#import "DeviceView.h"
#import "LoginController1.h"
@implementation ACController
@synthesize equipment;
@synthesize ctrOrder;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (id)initWithEquipment:(NewEquipment *)equ
{
    self = [super init];
    if (self) {
        // Custom initialization
        self.equipment = equ;
    }
    return self;
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    [self.view setFrame:CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH)];
    [self initTopBar];
    [self initLabel];
    [OnOff addTarget:self action:@selector(ctlDevice) forControlEvents:UIControlEventTouchUpInside];
    wind.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [up setTitle:NSLocalizedString(@"UP", nil) forState:UIControlStateNormal];
    [up setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [down setTitle:NSLocalizedString(@"DOWN", nil) forState:UIControlStateNormal];
    [down setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [autos setTitle:NSLocalizedString(@"AUTO", nil) forState:UIControlStateNormal];
    [autos setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [high setTitle:NSLocalizedString(@"HIGH", nil) forState:UIControlStateNormal];
    [high setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [middle setTitle:NSLocalizedString(@"MID", nil) forState:UIControlStateNormal];
    [middle setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [low setTitle:NSLocalizedString(@"LOW", nil) forState:UIControlStateNormal];
    [low setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    if([self.equipment.state isEqualToString: kStateON])
    {
        [DeviceView setDeivceOnOff:self.equipment button:OnOff State:YES];
    }
    else
    {
        [DeviceView setDeivceOnOff:self.equipment button:OnOff State:NO];
    }
}
- (void)initLabel
{
    [COOL setText:NSLocalizedString(@"COOL", nil)];
    [HOT setText:NSLocalizedString(@"HOT", nil)];
    [windSpeed setText:NSLocalizedString(@"windSpeed", nil)];
    [func setText:NSLocalizedString(@"FUNC", nil)];

}
- (void)viewDidUnload
{
    [autos release];
    autos = nil;
    [autos release];
    autos = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
//}
- (void)initTopBar
{    
    GKBNavigationBar *topBar = [[GKBNavigationBar alloc]init];
    [topBar setTarget:self returnAction:@selector(closeView) withTitle:self.equipment.name];
    [self.view addSubview:topBar];
    [topBar release];
    
}
- (void)closeView
{
    [GKBMainController refreshDevices];
    [self.view removeFromSuperview];
    [GKBMainController postStartRefresh];
}
#pragma mark - order
- (void)C16
{
    [self ctrAC:@"16"];
}
- (IBAction)C18
{
    [self ctrAC:@"18"];
}
- (IBAction)C20
{
    [self ctrAC:@"20"];
}
- (IBAction)C22
{
    [self ctrAC:@"22"];
}
- (IBAction)C24
{
    [self ctrAC:@"24"];
}
- (IBAction)C26
{
    [self ctrAC:@"26"];
}
- (IBAction)C28
{
    [self ctrAC:@"28"];
}
- (IBAction)C30
{
    [self ctrAC:@"30"];
}
- (IBAction)UPWIND
{
    [self ctrAC:@"UPWIND"];
}
- (IBAction)DOWNWIND
{
    [self ctrAC:@"DOWNWIND"];
}
- (IBAction)BLOWDOWN
{
    [self ctrAC:@"BLOWDOWN"];
}
- (IBAction)DEHUMIDIFIER
{
    [self ctrAC:@"DEHUMIDIFIER"];
}
- (IBAction)AUTO
{
    [self ctrAC:@"AUTO"];
}
- (IBAction)LOW
{
    [self ctrAC:@"LOW"];
}
- (IBAction)MID
{
    [self ctrAC:@"MID"];
}
- (IBAction)HIGH
{
    [self ctrAC:@"HIGH"];
}
- (IBAction)H24
{
    [self ctrAC:@"H24"];
}
- (IBAction)H26
{
    [self ctrAC:@"H26"];
}
- (IBAction)H28
{
    [self ctrAC:@"H28"];
}
- (IBAction)H30
{
    [self ctrAC:@"H30"];
}
#pragma mark - 电器设备开关状态
- (void)ctlDevice
{
    NSString *switchId = self.equipment.switchId;
    NSString *name = self.equipment.name;
    BOOL STATE = [DeviceView deviceStateOn:self.equipment];
    BOOL OPEN = !STATE;
    [DeviceView OperatSwithcId:switchId name:name OnOff:OPEN target:self end:@selector(ctlDeviceEnd:)];
}
- (void)ctlDeviceEnd:(NSDictionary *)dic
{
    NSLog(@"%@", dic);
    
    NSNumber *result = [dic objectForKey:@"result"];
    NSString *resultTip = @"";
    if (result.integerValue == 0) {   
        BOOL STATE = [DeviceView deviceStateOn:self.equipment ];
        BOOL OPEN = !STATE;
        //操作成功
        resultTip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        [DeviceView setDeivceOnOff:self.equipment button:OnOff State:OPEN];
        GKBMainController *main = [GKBMainController mainCtr];
        [main updateStateByNetTarget:self action:@selector(updateState:)];
        
    }
    else
    {
        //操作失败！原因：
        resultTip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
    }
    [iToast showTip:resultTip gravity:iToastGravityBottom];
}
- (void)updateState:(NSArray*)states
{
    [GKBMainController updateEqu:self.equipment button:OnOff states:states];
}

#pragma mark - 控制AC
- (void)ctrAC:(NSString *)ORDER
{
    NSString *order = ORDER;
    NSString *orderName = NSLocalizedString(order, nil);
    NSString *operat = NSLocalizedString(@"operat:", nil);
    NSString *startTip = [operat stringByAppendingString:orderName];
    [iToast showTip:startTip gravity:iToastGravityBottom];
    
    self.ctrOrder = order;
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    
    //操作失败！原因：
    NSString *errorTip = [OperationTip operatSuccess:NO type:ErrorTypeNetworkFail];
    [WebServices requestStartOrder:@"ctlAC" body:body target:self action:@selector(ctrACEnd:) errorTip:errorTip];
}
- (void)ctrACEnd:(NSDictionary *)dic
{
    NSNumber *result = [dic objectForKey:@"result"];
//    NSString *order = NSLocalizedString(self.ctrOrder, nil);
    if (result.integerValue == 0) {     
        //操作成功 
        NSString *tip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
    else
    {
        //操作失败！原因：
        NSString *tip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
}
- (void)dealloc {
[autos release];
    [wind release];
[super dealloc];
}
//- (void)dealloc {
//[autos release];
//[super dealloc];
//}
@end
